package com.ruoyi.recruit.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.constant.RecruitConstants;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.mapper.RecruitContentMapper;
import com.ruoyi.recruit.service.IRecruitContentService;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 * 招聘内容Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-17
 */
@Service
public class RecruitContentServiceImpl extends ServiceImpl<RecruitContentMapper, RecruitContent> implements IRecruitContentService
{
    /**
     * 查询招聘内容
     * 
     * @param contentId 招聘内容主键
     * @return 招聘内容
     */
    @Override
    public RecruitContent selectRecruitContentByContentId(Long contentId)
    {
        return baseMapper.selectRecruitContentByContentId(contentId);
    }

    /**
     * 查询招聘内容
     *
     * @param recruitId 招聘信息id
     * @return 招聘内容
     */
    @Override
    public List<RecruitContent> selectRecruitContentByRecruitId(Long recruitId)
    {
        return baseMapper.selectList(new LambdaQueryWrapper<RecruitContent>().eq(RecruitContent::getRecruitId, recruitId));
    }

    /**
     * 查询招聘内容列表
     * 
     * @param recruitContent 招聘内容
     * @return 招聘内容
     */
    @Override
    public List<RecruitContent> selectRecruitContentList(RecruitContent recruitContent)
    {
        return baseMapper.selectRecruitContentList(recruitContent);
    }

    /**
     * 新增招聘内容
     * 
     * @param recruitContent 招聘内容
     * @return 结果
     */
    @Override
    public int insertRecruitContent(RecruitContent recruitContent)
    {
        recruitContent.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(recruitContent);
    }

    /**
     * 修改招聘内容
     * 
     * @param recruitContent 招聘内容
     * @return 结果
     */
    @Override
    public int updateRecruitContent(RecruitContent recruitContent)
    {
        recruitContent.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recruitContent);
    }

    /**
     * 批量删除招聘内容
     * 
     * @param contentIds 需要删除的招聘内容主键
     * @return 结果
     */
    @Override
    public int deleteRecruitContentByContentIds(String contentIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(contentIds)));
    }

    /**
     * 删除招聘内容信息
     * 
     * @param contentId 招聘内容主键
     * @return 结果
     */
    @Override
    public int deleteRecruitContentByContentId(Long contentId)
    {
        return baseMapper.deleteById(contentId);
    }

    /**
     * 检验招聘计划唯一
     *
     * @param recruitContent 招聘计划
     * @return 结果
     */
    @Override
    public String checkRecruitContentUnique(RecruitContent recruitContent){
        Long contentId = StringUtils.isNull(recruitContent.getContentId()) ? -1L : recruitContent.getContentId();
        RecruitContent info = baseMapper.selectOne(new LambdaQueryWrapper<RecruitContent>()
                .eq(RecruitContent::getPostName, recruitContent.getPostName())
                .eq(RecruitContent::getPostCode, recruitContent.getPostCode())
                .eq(RecruitContent::getRecruitId, recruitContent.getRecruitId()).last("limit 1"));
        if (StringUtils.isNotNull(info) && info.getContentId().longValue() != contentId.longValue())
        {
            return RecruitConstants.CONTENT_TYPE_NOT_UNIQUE;
        }
        return RecruitConstants.CONTENT_TYPE_UNIQUE;
    }

    /**
     * 检查岗位名称和岗位代码是否存在
     * @param postName
     * @param postCode
     * @param recruitId
     * @return
     */
    @Override
    public RecruitContent checkRecruitContent(String postName, String postCode, Long recruitId){
        RecruitContent info = baseMapper.selectOne(new LambdaQueryWrapper<RecruitContent>()
                .eq(RecruitContent::getPostName, postName)
                .eq(RecruitContent::getPostCode, postCode)
                .eq(RecruitContent::getRecruitId, recruitId).last("limit 1"));
        return info;
    }

    /**
     * 导入招聘计划
     *
     * @param contentList 员工行政职务列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importContent(List<RecruitContent> contentList, Long recruitId, boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(contentList) || contentList.size() == 0)
        {
            throw new ServiceException("导入员工行政职务数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RecruitContent content : contentList)
        {
            int row = 1;
            try
            {
                content.setRecruitId(recruitId);
                //获取用户信息
                RecruitContent info = baseMapper.selectOne(new LambdaQueryWrapper<RecruitContent>()
                        .eq(RecruitContent::getPostName, content.getPostName())
                        .eq(RecruitContent::getPostCode, content.getPostCode())
                        .eq(RecruitContent::getRecruitId, content.getRecruitId()).last("limit 1"));
                if (StringUtils.isNull(info))
                {
                    content.setCreateBy(operName);
                    this.insertRecruitContent(content);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、第" + row + " 行招聘计划导入成功");
                }
                else if (isUpdateSupport)
                {
                    content.setContentId(info.getContentId());
                    content.setUpdateBy(operName);
                    this.updateRecruitContent(content);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、第" + row + " 行招聘计划更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + successNum + "、第：" + row + " 行招聘计划已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、第" + row + " 行招聘计划导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            row++;
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
